package com.jappit.runtracker.screens;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.jappit.runtracker.RunTracker;
import com.jappit.runtracker.data.DataManager;
import com.jappit.runtracker.data.RunSetHandler;
import com.jappit.runtracker.model.Run;
import com.jappit.runtracker.utils.AppUtils;
import com.jappit.runtracker.utils.FormatUtils;
import com.nokia.maps.common.GeoBoundingBox;
import com.nokia.maps.common.GeoCoordinate;
import com.nokia.maps.map.MapCanvas;
import com.nokia.maps.map.MapContainer;
import com.nokia.maps.map.MapMarker;
import com.nokia.maps.map.MapObject;
import com.nokia.maps.map.MapPolyline;
import com.nokia.maps.map.MapShapeType;
import com.nokia.maps.map.Point;
import com.nokia.maps.routing.GeoPosition;
import com.nokia.maps.routing.PositioningManager;

public class PastRunScreen extends MapCanvas implements CommandListener
{
	MapContainer pathContainer = null;
	
	Run run = null;
	
	Command detailCommand, backCommand;
	
	public PastRunScreen(Run run)
	{
		super(Display.getDisplay(AppUtils.getInstance().getMIDlet()));
		
		this.run = run;
		
		pathContainer = this.getMapFactory().createMapContainer(false);
        
		this.getMapDisplay().addMapObject(pathContainer);
		
		GeoBoundingBox bbox = this.run.getBoundingBox();
		
		this.getMapDisplay().zoomTo(bbox, false);
        
        this.setCommandListener(this);
        
        for(int i = 0; i < run.getCoordinatesNum() - 1; i++)
        {
        	MapPolyline line = this.getMapFactory().createMapPolyline(new GeoCoordinate[]{
        		run.getCoordinate(i), run.getCoordinate(i + 1)
        	});
        		
        	pathContainer.addMapObject(line);
        }
        MapObject startMarker, endMarker;
        try
        {
        	startMarker = this.getMapFactory().createMapMarker(run.getCoordinate(0), Image.createImage("/I_START.png"));
        	endMarker = this.getMapFactory().createMapMarker(run.getCoordinate(run.getCoordinatesNum() - 1), Image.createImage("/I_END.png"));
        	
        	((MapMarker)startMarker).setAnchor(new Point(-16, -16));
        	((MapMarker)endMarker).setAnchor(new Point(-16, -16));
        }
        catch(Exception e)
        {
        	startMarker = this.getMapFactory().createStandardMarker(run.getCoordinate(0), 32, "S", MapShapeType.baloon);
        	endMarker = this.getMapFactory().createStandardMarker(run.getCoordinate(run.getCoordinatesNum() - 1), 32, "F", MapShapeType.baloon);
        }
        this.getMapDisplay().addMapObject(startMarker);
        this.getMapDisplay().addMapObject(endMarker);
        
        this.addCommand(detailCommand = new Command("Details", Command.OK, 1));
        this.addCommand(backCommand = new Command("Back", Command.BACK, 1));
        setCommandListener(this);
    }
	protected void paint(Graphics graphics)
	{
		super.paint(graphics);
		
		Font mainFont = Font.getDefaultFont();
		int fontHeight = mainFont.getHeight();
		
		graphics.setColor(0x000000);
		graphics.fillRect(0, 0, getWidth(), fontHeight + 4);
		
		graphics.setColor(0xff0000);
		graphics.drawString(FormatUtils.formatDuration(run.getDuration()), 2, 2, Graphics.TOP | Graphics.LEFT);
		graphics.drawString(FormatUtils.formatDistance(run.getDistance()), getWidth() - 2, 2, Graphics.TOP | Graphics.RIGHT);
	}
    public void commandAction(Command c, Displayable d)
    {
    	if(c == backCommand)
    	{
    		AppUtils.getInstance().gotoPreviousScreen();
    	}
    	else if(c == detailCommand)
    	{
    		AppUtils.getInstance().gotoScreen(new RunDetailsScreen(run));
    	}
    }
}
